package com.ziyun.job.modules.job.task.order;

import com.baomidou.mybatisplus.plugins.Page;
import com.ziyun.erp.modules.member.entity.UserOrderCommissionEntity;
import com.ziyun.erp.modules.member.service.UserOrderCommissionService;
import com.ziyun.erp.modules.order.entity.ErpOrderEntity;
import com.ziyun.erp.modules.order.service.ErpOrderService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

/**
 * @author guizhao
 * @Description: 分享家佣金
 * @date 2018-09-03 10:49
 */
@Component("userOrderCommissionTask")
public class UserOrderCommissionTask {

    /**
     * 日志
     */
    private  static  Logger logger = LoggerFactory.getLogger(UserOrderCommissionTask.class);

    @Autowired
    private ErpOrderService erpOrderService;
    @Autowired
    private UserOrderCommissionService userOrderCommissionService;

    public void commissionCaculte(){
        // 佣金计算
//        userOrderCommission();
        // 佣金解冻
        commissionThawing();
    }

    /**
     * 佣金计算
     */
    public void userOrderCommission(){
        // 当前页数
        int currentPage = 1;

        Page<ErpOrderEntity> page = new Page<>(currentPage,50);
        // 查询 未计算佣金订单
        Page<ErpOrderEntity> list = erpOrderService.selectNotCalculateCommissionPage(page);

        // 总页数
        int totalPages = list.getPages();
        do {
            logger.info("紫云网未计算佣金订单---订单数量：" +list.getTotal()+"---每页订单数：50---总页数："+totalPages+"---当前页数："+currentPage);
            if(list != null && list.getTotal() > 0){
                logger.info(""+list.getRecords());
                erpOrderService.calculateCommission(list.getRecords());
            }
            currentPage++;
            if(currentPage <= totalPages){
                page = new Page<>(1,50);
                list = erpOrderService.selectNotCalculateCommissionPage(page);
            }
        }while (currentPage < totalPages);

        logger.info("同步紫云网订单佣金计算完成");
    }

    /**
     * 佣金冻结7天，解冻
     */
    private void commissionThawing() {
        // 当前页数
        int currentPage = 1;

        Page<UserOrderCommissionEntity> page = new Page<>(currentPage,50);
        // 查询 未计算佣金订单
        Page<UserOrderCommissionEntity> list = userOrderCommissionService.selectFreezeCommissionPage(page);

        // 总页数
        int totalPages = list.getPages();
        do {
            logger.info("紫云网需解冻佣金订单---订单数量：" +list.getTotal()+"---每页订单数：50---总页数："+totalPages+"---当前页数："+currentPage);
            if(list != null && list.getRecords().size()> 0){
                logger.info(""+list.getRecords());
                userOrderCommissionService.commissionThawing(list.getRecords());
            }
            currentPage++;
            if(currentPage <= totalPages){
                page = new Page<>(1,50);
                list = userOrderCommissionService.selectFreezeCommissionPage(page);
            }
        }while (currentPage < totalPages);

        logger.info("紫云网订单佣金解冻完成");
    }

}
